package UILayer;
import java.util.Iterator;
import java.util.Date;
import java.text.SimpleDateFormat;
import CtrLayer.ProductCtr;
import ModelLayer.Product;

public class ProductUI extends InOut
{

    private static int idP = 1000;
    private static ProductCtr productCtr = new ProductCtr();

    public ProductUI()
    {
        
    }

    public void productMenu()
    {
        String[] productMenu = {"Products MENU", "Add product", "Read item information", "Update item", "Delete item", "Add discount", "List products"};
        printMenu(productMenu);
    }
    
    public void updateProductMenu(int idP)
    {
        String[] updateProdMenu = {"Update Product with ID "+idP, "Update name", "Update description", "Update stock", "Update price", "Update location", "Add part", "Remove part"};
        printMenu(updateProdMenu);
    }

    public void start()
    {
        boolean exit = false;
        while(!exit)
        {
            erase(1);
            productMenu();
            int choice = inputInt("Choose: ");
            erase(1);
            switch(choice)
            {
                case 1 : {addProduct();}break;
                case 2 : {printProdInfo();}break;
                case 3 : {updateProduct();}break;
                case 4 : {removeProduct();}break;
                case 5 : {addDiscount();}break;
                case 6 : {listProducts();}break;
                case 7 : {exit=true;}break;
                
                default : {}; break;
            }
            if(choice>=1 && choice<=6 && choice!=3) waitK();
        }
    }
    
    
    public void listProducts()
    {
        Iterator it = productCtr.getProductsIterator();
        int k = 0;
        print("########## Product List ##########");
        while(it.hasNext())
        {
            Product prod = (Product)it.next();
            print(prod.getId()+" "+prod.getName());
            k++;
        }
        print("\n -> There are "+k+" products.");
    }
    
    public void updateProduct()
    {
        if(!haveAccess()) {waitK(); return;}
         
        int idP = inputInt("Write product's id: ");
        if(idP==-1) print(" -> Product ID invalid.");
        else
        {
            boolean exit = false;
            while(!exit)
            {
                erase(1);
                updateProductMenu(idP);
                int choice = inputInt("Choose: ");
                
                if(choice<1 || choice>7) exit=true;
                else
                {
                    String upInf = "New value: ";
                    if(choice == 6 || choice == 7) upInf = "Part product's ID: ";
                    updateProduct(idP,choice,upInf);
                    waitK();
                }
            }
        }
    }
    
    
    public void updateProduct(int idP, int opt, String upInf)
    {
        String newValue = inputString(upInf);
        if(productCtr.updateProduct(idP,newValue,opt))  print(" -> The product was successfully updated.");
        else print(" -> Product with id "+idP+" does not exist.");
    }
    
    
    public Product getProduct()
    {
        int idP = inputInt("Write product's id: ");
        return productCtr.getProduct(idP);
    }
    
    public void printProdInfo()
    {
        Product prod = getProduct();
        if(prod==null)
        {
            print(" -> That product does not exist");
        }
        else
        {
            print("######################");
            print("ID: "+prod.getId());
            print("Name: "+prod.getName());
            int j=prod.getSizeParts();
            if(j>0)
            {
                print("Parts: "+j);
                for(int i=0;i<j;i++)
                {
                    Product prod1 = prod.getPartFromId(i);
                    print("   "+prod1.getId()+" "+prod1.getName()+" "+prod1.getPrice());
                }
            }
            print("Description: "+prod.getDescription());
            print("Warehouse: "+prod.getLocation());
            print("Price: "+prod.getPrice());
            print("Discount: "+prod.getDiscount());
            print("Final price: "+prod.getFinalPrice());
            print("End date: "+prod.getEndDateDiscount());
        }
    }

    public void addProduct() 
    {
        idP++;
        String name = inputString("Write name: ");
        String desc = inputString("Write description: ");
        double price = inputDouble("Write price: ");
        if(price==-1) print(" -> Invalid price");
        else
        {
            String location = inputString("Write warehouse: ");
            productCtr.addProduct(idP,name,desc,price,location);
            print("The product was successfully added.");
        }
    }
    
    public void removeProduct()
    {
        int idP = inputInt("Write product's id: ");
        if(productCtr.removeProduct(idP)) print("The product with ID "+idP+" was removed.");
        else print("The product with ID "+idP+" does not exist.");
    }
    
    
    public void addDiscount()
    {
        if(!haveAccess()) return;
        
        SimpleDateFormat formatter = new SimpleDateFormat("dd/mm/yyyy");
        
        int idP = inputInt("Write product's id: ");
        double disc = inputDouble("Write discount in percents (e.g. 18%): ");
        if(disc>100) print("Discount cannot exceed 100%.");
        else
        {
            String dateInString = inputString("Write end date (e.g. 24/11/2013): ");
        
            try{
                Date endDate = formatter.parse(dateInString);
                if(productCtr.setDiscount(idP, disc, endDate)) print("The discount was successfully apllied to product with id "+idP);
                else print("The product with ID "+idP+" does not exist.");
            }
            catch(Exception e) { print("The date format is invalid."); }
        }
    }
}
